<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="code-ia4sE56m4l">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">
  
  <link rel="stylesheet" href="/blog/lib/animate-css/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"tangmonk-meat.gitee.io","root":"/blog/","scheme":"Muse","version":"8.0.0-rc.4","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"path":"search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>

  <meta name="description" content="SpringBoot+Druid+mybatis+log4j2+mysql8 1 基本概念Druid 是Java语言中最好的数据库连接池。 虽然 HikariCP 的速度稍快，但是，Druid能够提供强大的监控和扩展功能，也是阿里巴巴的开源项目。 Druid是阿里巴巴开发的号称为监控而生的数据库连接池，在功能、性能、扩展性方面，都超过其他数据库连接池，包括DBCP、C3P0、BoneCP、Prox">
<meta property="og:type" content="article">
<meta property="og:title" content="开启Druid监控统计功能(SQL监控、慢SQL记录、Spring监控、去广告)">
<meta property="og:url" content="https://tangmonk-meat.gitee.io/blog/2021/03/19/%E5%BC%80%E5%90%AFDruid%E7%9B%91%E6%8E%A7%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD(SQL%E7%9B%91%E6%8E%A7%E3%80%81%E6%85%A2SQL%E8%AE%B0%E5%BD%95%E3%80%81Spring%E7%9B%91%E6%8E%A7%E3%80%81%E5%8E%BB%E5%B9%BF%E5%91%8A)/index.html">
<meta property="og:site_name" content="唐僧肉BLOG">
<meta property="og:description" content="SpringBoot+Druid+mybatis+log4j2+mysql8 1 基本概念Druid 是Java语言中最好的数据库连接池。 虽然 HikariCP 的速度稍快，但是，Druid能够提供强大的监控和扩展功能，也是阿里巴巴的开源项目。 Druid是阿里巴巴开发的号称为监控而生的数据库连接池，在功能、性能、扩展性方面，都超过其他数据库连接池，包括DBCP、C3P0、BoneCP、Prox">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s1.ax1x.com/2020/08/11/aLbLY6.png">
<meta property="og:image" content="https://s1.ax1x.com/2020/08/11/aLLPu4.png">
<meta property="og:image" content="https://s1.ax1x.com/2020/08/11/aOWIkF.png">
<meta property="og:image" content="https://s1.ax1x.com/2020/08/11/aOWHp9.png">
<meta property="og:image" content="https://s1.ax1x.com/2020/08/11/aOfDnx.png">
<meta property="article:published_time" content="2021-03-19T13:16:54.902Z">
<meta property="article:modified_time" content="2020-09-23T02:30:07.929Z">
<meta property="article:author" content="TANGMONK-MEAT">
<meta property="article:tag" content="SpringBoot">
<meta property="article:tag" content="Spring">
<meta property="article:tag" content="java">
<meta property="article:tag" content="log4j2">
<meta property="article:tag" content="源码">
<meta property="article:tag" content="mybatis">
<meta property="article:tag" content="Druid">
<meta property="article:tag" content="mysql">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s1.ax1x.com/2020/08/11/aLbLY6.png">

<link rel="canonical" href="https://tangmonk-meat.gitee.io/blog/2021/03/19/%E5%BC%80%E5%90%AFDruid%E7%9B%91%E6%8E%A7%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD(SQL%E7%9B%91%E6%8E%A7%E3%80%81%E6%85%A2SQL%E8%AE%B0%E5%BD%95%E3%80%81Spring%E7%9B%91%E6%8E%A7%E3%80%81%E5%8E%BB%E5%B9%BF%E5%91%8A)/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>开启Druid监控统计功能(SQL监控、慢SQL记录、Spring监控、去广告) | 唐僧肉BLOG</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <main class="main">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader">
        <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">唐僧肉BLOG</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录生活中的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">44</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">17</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">38</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-number">1.</span> <span class="nav-text">1 基本概念</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E6%B7%BB%E5%8A%A0%E4%BE%9D%E8%B5%96"><span class="nav-number">2.</span> <span class="nav-text">2 添加依赖</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E9%85%8D%E7%BD%AE%E7%9B%B8%E5%85%B3%E5%B1%9E%E6%80%A7"><span class="nav-number">3.</span> <span class="nav-text">3 配置相关属性</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-%E5%A6%82%E4%BD%95%E9%85%8D%E7%BD%AE-Filter"><span class="nav-number">3.1.</span> <span class="nav-text">3.1 如何配置 Filter</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E7%9B%91%E6%8E%A7%E9%A1%B5%E9%9D%A2"><span class="nav-number">4.</span> <span class="nav-text">3 监控页面</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-sql%E7%9B%91%E6%8E%A7"><span class="nav-number">5.</span> <span class="nav-text">4 sql监控</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-%E6%85%A2sql%E8%AE%B0%E5%BD%95"><span class="nav-number">6.</span> <span class="nav-text">5 慢sql记录</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#6-spring-%E7%9B%91%E6%8E%A7"><span class="nav-number">7.</span> <span class="nav-text">6 spring 监控</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#7-%E5%8E%BB-Ad%EF%BC%88%E5%B9%BF%E5%91%8A%EF%BC%89"><span class="nav-number">8.</span> <span class="nav-text">7 去 Ad（广告）</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#8-%E8%8E%B7%E5%8F%96-Druid-%E7%9A%84%E7%9B%91%E6%8E%A7%E6%95%B0%E6%8D%AE"><span class="nav-number">9.</span> <span class="nav-text">8 获取 Druid 的监控数据</span></a></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="TANGMONK-MEAT"
      src="https://s1.ax1x.com/2020/09/22/wOscW9.jpg">
  <p class="site-author-name" itemprop="name">TANGMONK-MEAT</p>
  <div class="site-description" itemprop="description">直到这一刻微笑着说话为止，我至少留下了一公升眼泪</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">44</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/TANGMONK-MEAT" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;TANGMONK-MEAT" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2950371251@qq.com" title="E-Mail → mailto:2950371251@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/TANGMONK-MEAT" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;TANGMONK-MEAT" rel="noopener" target="_blank"><i class="iconfont icongitee-fill-round fa-fw"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.leancloud.cn/" title="LeanCloud → https:&#x2F;&#x2F;www.leancloud.cn" rel="noopener" target="_blank"><i class="iconfont iconCloudupload fa-fw"></i>LeanCloud</a>
      </span>
  </div>
  <div class="cc-license animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" class="cc-opacity" rel="noopener" target="_blank"><img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </section>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </header>

      
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/TANGMONK-MEAT" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div id="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


      <div class="main-inner">
        

        <div class="content post posts-expand">
          

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://tangmonk-meat.gitee.io/blog/2021/03/19/%E5%BC%80%E5%90%AFDruid%E7%9B%91%E6%8E%A7%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD(SQL%E7%9B%91%E6%8E%A7%E3%80%81%E6%85%A2SQL%E8%AE%B0%E5%BD%95%E3%80%81Spring%E7%9B%91%E6%8E%A7%E3%80%81%E5%8E%BB%E5%B9%BF%E5%91%8A)/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://s1.ax1x.com/2020/09/22/wOscW9.jpg">
      <meta itemprop="name" content="TANGMONK-MEAT">
      <meta itemprop="description" content="直到这一刻微笑着说话为止，我至少留下了一公升眼泪">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="唐僧肉BLOG">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          开启Druid监控统计功能(SQL监控、慢SQL记录、Spring监控、去广告)
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-19 21:16:54" itemprop="dateCreated datePublished" datetime="2021-03-19T21:16:54+08:00">2021-03-19</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-09-23 10:30:07" itemprop="dateModified" datetime="2020-09-23T10:30:07+08:00">2020-09-23</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/SpringBoot/" itemprop="url" rel="index"><span itemprop="name">SpringBoot</span></a>
                </span>
            </span>

          
            <span id="/blog/2021/03/19/%E5%BC%80%E5%90%AFDruid%E7%9B%91%E6%8E%A7%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD(SQL%E7%9B%91%E6%8E%A7%E3%80%81%E6%85%A2SQL%E8%AE%B0%E5%BD%95%E3%80%81Spring%E7%9B%91%E6%8E%A7%E3%80%81%E5%8E%BB%E5%B9%BF%E5%91%8A)/" class="post-meta-item leancloud_visitors" data-flag-title="开启Druid监控统计功能(SQL监控、慢SQL记录、Spring监控、去广告)" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/blog/2021/03/19/%E5%BC%80%E5%90%AFDruid%E7%9B%91%E6%8E%A7%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD(SQL%E7%9B%91%E6%8E%A7%E3%80%81%E6%85%A2SQL%E8%AE%B0%E5%BD%95%E3%80%81Spring%E7%9B%91%E6%8E%A7%E3%80%81%E5%8E%BB%E5%B9%BF%E5%91%8A)/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/blog/2021/03/19/%E5%BC%80%E5%90%AFDruid%E7%9B%91%E6%8E%A7%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD(SQL%E7%9B%91%E6%8E%A7%E3%80%81%E6%85%A2SQL%E8%AE%B0%E5%BD%95%E3%80%81Spring%E7%9B%91%E6%8E%A7%E3%80%81%E5%8E%BB%E5%B9%BF%E5%91%8A)/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>9.9k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>9 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p><strong>SpringBoot+Druid+mybatis+log4j2+mysql8</strong></p>
<h1 id="1-基本概念"><a href="#1-基本概念" class="headerlink" title="1 基本概念"></a>1 基本概念</h1><p><strong>Druid</strong> 是Java语言中最好的数据库连接池。</p>
<p>虽然 <strong>HikariCP</strong> 的速度稍快，但是，<strong>Druid能够提供强大的监控和扩展功能</strong>，也是阿里巴巴的开源项目。</p>
<p>Druid是阿里巴巴开发的号称为监控而生的数据库连接池，在功能、性能、扩展性方面，都超过其他数据库连接池，包括DBCP、C3P0、BoneCP、Proxool、JBoss DataSource等等等，秒杀一切。</p>
<p>Druid 可以很好的监控 DB 池连接和 SQL 的执行情况，天生就是针对监控而生的 DB 连接池。</p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/wangmx1993328/article/details/81834974">Spring Boot 默认数据源 HikariDataSource 与 JdbcTemplate</a>中已经介绍 <strong>Spring Boot 2.x 默认使用 Hikari 数据源</strong>，可以说 Hikari 与 Driud 都是当前 Java Web 上最优秀的数据源。</p>
<p>而Druid已经在阿里巴巴部署了超过600个应用，经过好几年生产环境大规模部署的严苛考验！</p>
<ul>
<li><p>stat：Druid内置提供一个<code>StatFilter</code>,用于统计监控信息。</p>
</li>
<li><p>wall：Druid防御SQL注入攻击的<code>WallFilter</code>就是通过Druid的SQL Parser分析。Druid提供的SQL Parser可以在JDBC层拦截SQL做相应处理，比如说分库分表、审计等。</p>
</li>
<li><p>log4j2：这个就是 日志记录的功能，可以把sql语句打印到log4j2 供排查问题。</p>
</li>
</ul>
<a id="more"></a>

<h1 id="2-添加依赖"><a href="#2-添加依赖" class="headerlink" title="2 添加依赖"></a>2 添加依赖</h1><blockquote>
<p>pom.xml</p>
</blockquote>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 阿里巴巴的druid数据源 --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>druid-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.1.23<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- mysql8 驱动--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">scope</span>&gt;</span>runtime<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--使用 log4j2 记录日志--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-log4j2<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">mybatis，引入了 SpringBoot的 JDBC 模块，</span></span><br><span class="line"><span class="comment">所以，默认是使用 hikari 作为数据源</span></span><br><span class="line"><span class="comment">--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>2.1.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 排除默认的 HikariCP 数据源 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.zaxxer<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>HikariCP<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h1 id="3-配置相关属性"><a href="#3-配置相关属性" class="headerlink" title="3 配置相关属性"></a>3 配置相关属性</h1><ul>
<li><p><strong>配置Druid数据源（连接池）</strong>： 如同以前 c3p0、dbcp 数据源可以设置数据源连接初始化大小、最大连接数、等待时间、最小连接数 等一样，Druid 数据源同理可以进行设置；</p>
</li>
<li><p><strong>配置 Druid web 监控 filter（<code>WebStatFilter</code>）</strong>： 这个过滤器的作用就是统计 web 应用请求中所有的数据库信息，比如 发出的 sql 语句，sql 执行的时间、请求次数、请求的 url 地址、以及seesion 监控、数据库表的访问次数 等等。 </p>
</li>
<li><p><strong>配置 Druid 后台管理 Servlet（<code>StatViewServlet</code>）</strong>： Druid 数据源具有监控的功能，并提供了一个 web 界面方便用户查看，类似安装 路由器 时，人家也提供了一个默认的 web 页面；需要设置 Druid 的后台管理页面的属性，比如 登录账号、密码 等；</p>
</li>
</ul>
<p><strong>注意：</strong></p>
<p>Druid Spring Boot Starter 配置属性的名称完全遵照 Druid，可以通过 Spring Boot 配置文件来配置Druid数据库连接池和监控，如果没有配置则使用默认值。</p>
<blockquote>
<p>application.yml</p>
</blockquote>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">########## 配置数据源 （Druid）##########</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">datasource:</span></span><br><span class="line">    <span class="comment">########## JDBC 基本配置 ##########</span></span><br><span class="line">    <span class="attr">username:</span> <span class="string">xxx</span></span><br><span class="line">    <span class="attr">password:</span> <span class="string">xxx</span></span><br><span class="line">    <span class="attr">driver-class-name:</span> <span class="string">com.mysql.cj.jdbc.Driver</span>   <span class="comment"># mysql8 的连接驱动</span></span><br><span class="line">    <span class="attr">url:</span> <span class="string">jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai</span></span><br><span class="line">    <span class="attr">platform:</span> <span class="string">mysql</span>                               <span class="comment"># 数据库类型</span></span><br><span class="line">    <span class="attr">type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span>  <span class="comment"># 指定数据源类型</span></span><br><span class="line">    <span class="comment">########## 连接池 配置 ##########</span></span><br><span class="line">    <span class="attr">druid:</span></span><br><span class="line">      <span class="comment"># 配置初始化大小、最小、最大</span></span><br><span class="line">      <span class="attr">initial-size:</span> <span class="number">5</span></span><br><span class="line">      <span class="attr">minIdle:</span> <span class="number">10</span></span><br><span class="line">      <span class="attr">max-active:</span> <span class="number">20</span></span><br><span class="line">      <span class="comment"># 配置获取连接等待超时的时间(单位：毫秒)</span></span><br><span class="line">      <span class="attr">max-wait:</span> <span class="number">60000</span></span><br><span class="line">      <span class="comment"># 配置间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒</span></span><br><span class="line">      <span class="attr">time-between-eviction-runs-millis:</span> <span class="number">2000</span></span><br><span class="line">      <span class="comment"># 配置一个连接在池中最小生存的时间，单位是毫秒</span></span><br><span class="line">      <span class="attr">min-evictable-idle-time-millis:</span> <span class="number">600000</span></span><br><span class="line">      <span class="attr">max-evictable-idle-time-millis:</span> <span class="number">900000</span></span><br><span class="line">      <span class="comment"># 用来测试连接是否可用的SQL语句,默认值每种数据库都不相同,这是mysql</span></span><br><span class="line">      <span class="attr">validationQuery:</span> <span class="string">select</span> <span class="number">1</span></span><br><span class="line">      <span class="comment"># 应用向连接池申请连接，并且testOnBorrow为false时，连接池将会判断连接是否处于空闲状态，如果是，则验证这条连接是否可用</span></span><br><span class="line">      <span class="attr">testWhileIdle:</span> <span class="literal">true</span></span><br><span class="line">      <span class="comment"># 如果为true，默认是false，应用向连接池申请连接时，连接池会判断这条连接是否是可用的</span></span><br><span class="line">      <span class="attr">testOnBorrow:</span> <span class="literal">false</span></span><br><span class="line">      <span class="comment"># 如果为true（默认false），当应用使用完连接，连接池回收连接的时候会判断该连接是否还可用</span></span><br><span class="line">      <span class="attr">testOnReturn:</span> <span class="literal">false</span></span><br><span class="line">      <span class="comment"># 是否缓存preparedStatement，也就是PSCache。PSCache对支持游标的数据库性能提升巨大，比如说oracle</span></span><br><span class="line">      <span class="attr">poolPreparedStatements:</span> <span class="literal">true</span></span><br><span class="line">      <span class="comment"># 要启用PSCache，必须配置大于0，当大于0时， poolPreparedStatements自动触发修改为true，</span></span><br><span class="line">      <span class="comment"># 在Druid中，不会存在Oracle下PSCache占用内存过多的问题，</span></span><br><span class="line">      <span class="comment"># 可以把这个数值配置大一些，比如说100</span></span><br><span class="line">      <span class="attr">maxOpenPreparedStatements:</span> <span class="number">20</span></span><br><span class="line">      <span class="comment"># 连接池中的minIdle数量以内的连接，空闲时间超过minEvictableIdleTimeMillis，则会执行keepAlive操作</span></span><br><span class="line">      <span class="attr">keepAlive:</span> <span class="literal">true</span></span><br><span class="line">      <span class="comment"># Spring 监控，利用aop 对指定接口的执行时间，jdbc数进行记录</span></span><br><span class="line">      <span class="attr">aop-patterns:</span> <span class="string">&quot;com.springboot.template.dao.*&quot;</span></span><br><span class="line">      <span class="comment">########### 启用内置过滤器（第一个 stat必须，否则监控不到SQL）##########</span></span><br><span class="line">      <span class="attr">filters:</span> <span class="string">stat,wall,log4j2</span></span><br><span class="line">      <span class="comment"># 自己配置监控统计拦截的filter</span></span><br><span class="line">      <span class="attr">filter:</span></span><br><span class="line">        <span class="comment"># 开启druiddatasource的状态监控</span></span><br><span class="line">        <span class="attr">stat:</span></span><br><span class="line">          <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line">          <span class="attr">db-type:</span> <span class="string">mysql</span></span><br><span class="line">          <span class="comment"># 开启慢sql监控，超过2s 就认为是慢sql，记录到日志中</span></span><br><span class="line">          <span class="attr">log-slow-sql:</span> <span class="literal">true</span></span><br><span class="line">          <span class="attr">slow-sql-millis:</span> <span class="number">2000</span></span><br><span class="line">        <span class="comment"># 日志监控，使用slf4j 进行日志输出</span></span><br><span class="line">        <span class="attr">slf4j:</span></span><br><span class="line">          <span class="attr">enabled:</span> <span class="literal">true</span></span><br><span class="line">          <span class="attr">statement-log-error-enabled:</span> <span class="literal">true</span></span><br><span class="line">          <span class="attr">statement-create-after-log-enabled:</span> <span class="literal">false</span></span><br><span class="line">          <span class="attr">statement-close-after-log-enabled:</span> <span class="literal">false</span></span><br><span class="line">          <span class="attr">result-set-open-after-log-enabled:</span> <span class="literal">false</span></span><br><span class="line">          <span class="attr">result-set-close-after-log-enabled:</span> <span class="literal">false</span></span><br><span class="line">      <span class="comment">########## 配置WebStatFilter，用于采集web关联监控的数据 ##########</span></span><br><span class="line">      <span class="attr">web-stat-filter:</span></span><br><span class="line">        <span class="attr">enabled:</span> <span class="literal">true</span>                   <span class="comment"># 启动 StatFilter</span></span><br><span class="line">        <span class="attr">url-pattern:</span> <span class="string">/*</span>                 <span class="comment"># 过滤所有url</span></span><br><span class="line">        <span class="attr">exclusions:</span> <span class="string">&quot;*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*&quot;</span> <span class="comment"># 排除一些不必要的url</span></span><br><span class="line">        <span class="attr">session-stat-enable:</span> <span class="literal">true</span>       <span class="comment"># 开启session统计功能</span></span><br><span class="line">        <span class="attr">session-stat-max-count:</span> <span class="number">1000</span>    <span class="comment"># session的最大个数,默认100</span></span><br><span class="line">      <span class="comment">########## 配置StatViewServlet（监控页面），用于展示Druid的统计信息 ##########</span></span><br><span class="line">      <span class="attr">stat-view-servlet:</span></span><br><span class="line">        <span class="attr">enabled:</span> <span class="literal">true</span>                   <span class="comment"># 启用StatViewServlet</span></span><br><span class="line">        <span class="attr">url-pattern:</span> <span class="string">/druid/*</span>           <span class="comment"># 访问内置监控页面的路径，内置监控页面的首页是/druid/index.html</span></span><br><span class="line">        <span class="attr">reset-enable:</span> <span class="literal">false</span>              <span class="comment"># 不允许清空统计数据,重新计算</span></span><br><span class="line">        <span class="attr">login-username:</span> <span class="string">root</span>            <span class="comment"># 配置监控页面访问密码</span></span><br><span class="line">        <span class="attr">login-password:</span> <span class="number">123</span></span><br><span class="line">        <span class="attr">allow:</span> <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>           <span class="comment"># 允许访问的地址，如果allow没有配置或者为空，则允许所有访问</span></span><br><span class="line">        <span class="attr">deny:</span>                                        <span class="comment"># 拒绝访问的地址，deny优先于allow，如果在deny列表中，就算在allow列表中，也会被拒绝</span></span><br></pre></td></tr></table></figure>

<p>上述配置文件的参数可以在 <code>com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties</code> 和 <code>org.springframework.boot.autoconfigure.jdbc.DataSourceProperties</code>中找到;</p>
<h2 id="3-1-如何配置-Filter"><a href="#3-1-如何配置-Filter" class="headerlink" title="3.1 如何配置 Filter"></a>3.1 如何配置 Filter</h2><p>可以通过 <code>spring.datasource.druid.filters=stat,wall,log4j ... </code>的方式来启用相应的内置Filter，不过这些Filter都是默认配置。如果默认配置不能满足需求，可以放弃这种方式，通过配置文件来配置Filter，下面是例子。</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 配置StatFilter </span></span><br><span class="line"><span class="meta">spring.datasource.druid.filter.stat.enabled</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">spring.datasource.druid.filter.stat.db-type</span>=<span class="string">h2</span></span><br><span class="line"><span class="meta">spring.datasource.druid.filter.stat.log-slow-sql</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">spring.datasource.druid.filter.stat.slow-sql-millis</span>=<span class="string">2000</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 配置WallFilter </span></span><br><span class="line"><span class="meta">spring.datasource.druid.filter.wall.enabled</span>=<span class="string">true</span></span><br><span class="line"><span class="meta">spring.datasource.druid.filter.wall.db-type</span>=<span class="string">h2</span></span><br><span class="line"><span class="meta">spring.datasource.druid.filter.wall.config.delete-allow</span>=<span class="string">false</span></span><br><span class="line"><span class="meta">spring.datasource.druid.filter.wall.config.drop-table-allow</span>=<span class="string">false</span></span><br></pre></td></tr></table></figure>

<p>目前为以下 Filter 提供了配置支持，根据（spring.datasource.druid.filter.*）进行配置。</p>
<ul>
<li>StatFilter</li>
<li>WallFilter</li>
<li>ConfigFilter</li>
<li>EncodingConvertFilter</li>
<li>Slf4jLogFilter</li>
<li>Log4jFilter</li>
<li>Log4j2Filter</li>
<li>CommonsLogFilter</li>
</ul>
<p>不想使用内置的 Filters，要想使自定义 Filter 配置生效需要将对应 Filter 的 enabled 设置为 true ，Druid Spring Boot Starter 默认禁用 StatFilter，可以将其 enabled 设置为 true 来启用它。</p>
<h1 id="3-监控页面"><a href="#3-监控页面" class="headerlink" title="3 监控页面"></a>3 监控页面</h1><p>（1）启动项目后，访问 <code>/druid/login.html</code> 来到<strong>登录页面</strong>，输入用户名密码登录</p>
<p><img data-src="https://s1.ax1x.com/2020/08/11/aLbLY6.png" alt="aLbLY6.png"></p>
<p>（2）<strong>数据源页面</strong> 是当前DataSource配置的基本信息，上述配置的Filter可以在里面找到，如果没有配置Filter（一些信息会无法统计，例如“SQL监控”，会无法获取JDBC相关的SQL执行信息）</p>
<p><img data-src="https://s1.ax1x.com/2020/08/11/aLLPu4.png" alt="aLLPu4.png"></p>
<p>（3） <strong>SQL监控页面</strong>，统计了所有SQL语句的执行情况</p>
<p><img data-src="https://s1.ax1x.com/2020/08/11/aOWIkF.png" alt="aOWIkF.png"></p>
<p>（4）<strong>URL监控页面</strong>，统计了所有Controller接口的访问以及执行情况</p>
<p><img data-src="https://s1.ax1x.com/2020/08/11/aOWHp9.png" alt="aOWHp9.png">(<a target="_blank" rel="noopener" href="https://imgchr.com/i/aOWHp9">https://imgchr.com/i/aOWHp9</a>)</p>
<p>（5）Spring 监控页面，利用aop 对指定接口的执行时间，jdbc数进行记录</p>
<p><img data-src="https://s1.ax1x.com/2020/08/11/aOfDnx.png" alt="aOfDnx.png"></p>
<p>（6）<strong>SQL防火墙页面</strong></p>
<p>druid提供了黑白名单的访问，可以清楚的看到sql防护情况。</p>
<p>（7）<strong>Session监控页面</strong></p>
<p>可以看到当前的session状况，创建时间、最后活跃时间、请求次数、请求时间等详细参数。</p>
<p>（8）<strong>JSONAPI 页面</strong></p>
<p>通过api的形式访问Druid的监控接口，api接口返回Json形式数据。</p>
<h1 id="4-sql监控"><a href="#4-sql监控" class="headerlink" title="4 sql监控"></a>4 sql监控</h1><p>配置 Druid web 监控 filter（<code>WebStatFilter</code>）这个过滤器，作用就是统计 web 应用请求中所有的数据库信息，比如 发出的 sql 语句，sql 执行的时间、请求次数、请求的 url 地址、以及seesion 监控、数据库表的访问次数 等等。 </p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">datasource:</span></span><br><span class="line">    <span class="attr">druid:</span></span><br><span class="line">      <span class="comment">########## 配置WebStatFilter，用于采集web关联监控的数据 ##########</span></span><br><span class="line">      <span class="attr">web-stat-filter:</span></span><br><span class="line">        <span class="attr">enabled:</span> <span class="literal">true</span>                   <span class="comment"># 启动 StatFilter</span></span><br><span class="line">        <span class="attr">url-pattern:</span> <span class="string">/*</span>                 <span class="comment"># 过滤所有url</span></span><br><span class="line">        <span class="attr">exclusions:</span> <span class="string">&quot;*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*&quot;</span> <span class="comment"># 排除一些不必要的url</span></span><br><span class="line">        <span class="attr">session-stat-enable:</span> <span class="literal">true</span>       <span class="comment"># 开启session统计功能</span></span><br><span class="line">        <span class="attr">session-stat-max-count:</span> <span class="number">1000</span>    <span class="comment"># session的最大个数,默认100</span></span><br></pre></td></tr></table></figure>

<h1 id="5-慢sql记录"><a href="#5-慢sql记录" class="headerlink" title="5 慢sql记录"></a>5 慢sql记录</h1><p>有时候，系统中有些SQL执行很慢，我们希望使用日志记录下来，可以开启Druid的慢SQL记录功能</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line">  <span class="attr">datasource:</span></span><br><span class="line">    <span class="attr">druid:</span></span><br><span class="line">      <span class="attr">filter:</span></span><br><span class="line">        <span class="attr">stat:</span></span><br><span class="line">          <span class="attr">enabled:</span> <span class="literal">true</span>         <span class="comment"># 开启DruidDataSource状态监控</span></span><br><span class="line">          <span class="attr">db-type:</span> <span class="string">mysql</span>        <span class="comment"># 数据库的类型</span></span><br><span class="line">          <span class="attr">log-slow-sql:</span> <span class="literal">true</span>    <span class="comment"># 开启慢SQL记录功能</span></span><br><span class="line">          <span class="attr">slow-sql-millis:</span> <span class="number">2000</span> <span class="comment"># 默认3000毫秒，这里超过2s，就是慢，记录到日志</span></span><br></pre></td></tr></table></figure>

<p>启动后，如果遇到执行慢的SQL，便会输出到日志中</p>
<h1 id="6-spring-监控"><a href="#6-spring-监控" class="headerlink" title="6 spring 监控"></a>6 spring 监控</h1><p>访问之后spring监控默认是没有数据的；</p>
<p>这需要导入SprngBoot的AOP的Starter</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--SpringBoot 的aop 模块--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-aop<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>需要在 application.yml 配置:</p>
<blockquote>
<p>spring.datasource.druid.aop-patterns=”com.springboot.template.dao.*”</p>
<p>Spring监控AOP切入点，如com.springboot.template.dao.*,配置多个英文逗号分隔</p>
</blockquote>
<h1 id="7-去-Ad（广告）"><a href="#7-去-Ad（广告）" class="headerlink" title="7 去 Ad（广告）"></a>7 去 Ad（广告）</h1><p>访问监控页面的时候，你可能会在页面底部（footer）看到阿里巴巴的广告</p>
<p>原因：引入的druid的jar包中的<code>common.js</code>(里面有一段js代码是给页面的footer追加广告的)</p>
<p>如果想去掉，有两种方式：</p>
<p><strong>(1) 直接手动注释这段代码</strong></p>
<p>如果是使用Maven，直接到本地仓库中，查找这个jar包</p>
<p>要注释的代码：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// this.buildFooter();</span></span><br></pre></td></tr></table></figure>

<p>common.js的位置：</p>
<blockquote>
<p>com/alibaba/druid/1.1.23/druid-1.1.23.jar!/support/http/resources/js/common.js</p>
</blockquote>
<p><strong>(2) 使用过滤器过滤</strong></p>
<p>注册一个过滤器，过滤common.js的请求，使用正则表达式替换相关的广告内容</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="meta">@ConditionalOnWebApplication</span></span><br><span class="line"><span class="meta">@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)</span></span><br><span class="line"><span class="meta">@ConditionalOnProperty(name = &quot;spring.datasource.druid.stat-view-servlet.enabled&quot;,</span></span><br><span class="line"><span class="meta">havingValue = &quot;true&quot;, matchIfMissing = true)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RemoveDruidAdConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">    * 方法名: removeDruidAdFilterRegistrationBean</span></span><br><span class="line"><span class="comment">    * 方法描述 除去页面底部的广告</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@param</span> properties com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties</span></span><br><span class="line"><span class="comment">    * <span class="doctag">@return</span> org.springframework.boot.web.servlet.FilterRegistrationBean</span></span><br><span class="line"><span class="comment">    */</span></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> FilterRegistrationBean <span class="title">removeDruidAdFilterRegistrationBean</span><span class="params">(DruidStatProperties properties)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 获取web监控页面的参数</span></span><br><span class="line">        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();</span><br><span class="line">        <span class="comment">// 提取common.js的配置路径</span></span><br><span class="line">        String pattern = config.getUrlPattern() != <span class="keyword">null</span> ? config.getUrlPattern() : <span class="string">&quot;/druid/*&quot;</span>;</span><br><span class="line">        String commonJsPattern = pattern.replaceAll(<span class="string">&quot;\\*&quot;</span>, <span class="string">&quot;js/common.js&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">final</span> String filePath = <span class="string">&quot;support/http/resources/js/common.js&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建filter进行过滤</span></span><br><span class="line">        Filter filter = <span class="keyword">new</span> Filter() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">init</span><span class="params">(FilterConfig filterConfig)</span> <span class="keyword">throws</span> ServletException </span>&#123;&#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">doFilter</span><span class="params">(ServletRequest request, ServletResponse response, FilterChain chain)</span> <span class="keyword">throws</span> IOException, ServletException </span>&#123;</span><br><span class="line">                chain.doFilter(request, response);</span><br><span class="line">                <span class="comment">// 重置缓冲区，响应头不会被重置</span></span><br><span class="line">                response.resetBuffer();</span><br><span class="line">                <span class="comment">// 获取common.js</span></span><br><span class="line">                String text = Utils.readFromResource(filePath);</span><br><span class="line">                <span class="comment">// 正则替换banner, 除去底部的广告信息</span></span><br><span class="line">                text = text.replaceAll(<span class="string">&quot;&lt;a.*?banner\&quot;&gt;&lt;/a&gt;&lt;br/&gt;&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">                text = text.replaceAll(<span class="string">&quot;powered.*?shrek.wang&lt;/a&gt;&quot;</span>, <span class="string">&quot;&quot;</span>);</span><br><span class="line">                response.getWriter().write(text);</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">destroy</span><span class="params">()</span> </span>&#123;&#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        FilterRegistrationBean registrationBean = <span class="keyword">new</span> FilterRegistrationBean();</span><br><span class="line">        registrationBean.setFilter(filter);</span><br><span class="line">        registrationBean.addUrlPatterns(commonJsPattern);</span><br><span class="line">        <span class="keyword">return</span> registrationBean;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>两种方式都可以，建议使用的是第一种，从根源解决</p>
<h1 id="8-获取-Druid-的监控数据"><a href="#8-获取-Druid-的监控数据" class="headerlink" title="8 获取 Druid 的监控数据"></a>8 获取 Druid 的监控数据</h1><p>Druid 的监控数据可以在 <strong>开启 StatFilter 后</strong>，通过 <code>DruidStatManagerFacade</code> 进行获取;</p>
<p><code>DruidStatManagerFacade#getDataSourceStatDataList</code> 该方法可以获取所有数据源的监控数据，</p>
<p>除此之外 <code>DruidStatManagerFacade</code> 还提供了一些其他方法，可以按需选择使用。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="meta">@RequestMapping(value = &quot;/druid&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DruidStatController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@GetMapping(&quot;/stat&quot;)</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">druidStat</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="comment">// 获取数据源的监控数据</span></span><br><span class="line">        <span class="keyword">return</span> DruidStatManagerFacade.getInstance().getDataSourceStatDataList();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>参考</p>
<p><a target="_blank" rel="noopener" href="https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98">github Druid源码</a></p>
<p><a target="_blank" rel="noopener" href="https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter">github druid-spring-boot-starter源码</a></p>
<p><a target="_blank" rel="noopener" href="https://github.com/alibaba/druid/wiki/%E9%A6%96%E9%A1%B5">Druid Wiki</a></p>
<p><a target="_blank" rel="noopener" href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#data-properties">springboot#data-properties</a></p>
<p><a target="_blank" rel="noopener" href="http://m.hangge.com/news/cache/detail_2876.html">参考博客</a></p>

    </div>

     <!-- 文章结束表示语-->
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
      
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>TANGMONK-MEAT
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://tangmonk-meat.gitee.io/blog/2021/03/19/%E5%BC%80%E5%90%AFDruid%E7%9B%91%E6%8E%A7%E7%BB%9F%E8%AE%A1%E5%8A%9F%E8%83%BD(SQL%E7%9B%91%E6%8E%A7%E3%80%81%E6%85%A2SQL%E8%AE%B0%E5%BD%95%E3%80%81Spring%E7%9B%91%E6%8E%A7%E3%80%81%E5%8E%BB%E5%B9%BF%E5%91%8A)/" title="开启Druid监控统计功能(SQL监控、慢SQL记录、Spring监控、去广告)">https://tangmonk-meat.gitee.io/blog/2021/03/19/开启Druid监控统计功能(SQL监控、慢SQL记录、Spring监控、去广告)/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/SpringBoot/" rel="tag"><i class="fa fa-tag"></i> SpringBoot</a>
              <a href="/blog/tags/Spring/" rel="tag"><i class="fa fa-tag"></i> Spring</a>
              <a href="/blog/tags/java/" rel="tag"><i class="fa fa-tag"></i> java</a>
              <a href="/blog/tags/log4j2/" rel="tag"><i class="fa fa-tag"></i> log4j2</a>
              <a href="/blog/tags/%E6%BA%90%E7%A0%81/" rel="tag"><i class="fa fa-tag"></i> 源码</a>
              <a href="/blog/tags/mybatis/" rel="tag"><i class="fa fa-tag"></i> mybatis</a>
              <a href="/blog/tags/Druid/" rel="tag"><i class="fa fa-tag"></i> Druid</a>
              <a href="/blog/tags/mysql/" rel="tag"><i class="fa fa-tag"></i> mysql</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2021/03/19/ubuntu%E6%9D%80%E6%AD%BB%E5%83%B5%E6%AD%BB%E8%BF%9B%E7%A8%8B/" rel="prev" title="ubuntu杀死僵死进程">
      <i class="fa fa-chevron-left"></i> ubuntu杀死僵死进程
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2021/03/19/%E9%85%8D%E7%BD%AETomcat%E7%BD%91%E9%A1%B5%E7%AE%A1%E7%90%86%E8%AE%BF%E9%97%AE/" rel="next" title="配置Tomcat网页管理访问 - Tomcat">
      配置Tomcat网页管理访问 - Tomcat <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



        </div>
        
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">TANGMONK-MEAT</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">148k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">2:15</span>
</div>

<!--
-->

<!--<div>
<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span> 
 <span class="post-meta-divider">|</span> 
 <span class="post-meta-item-icon">
<span id="busuanzi_container_site_uv">
  本站访客数<span id="busuanzi_value_site_uv"></span>人次
</span>
</div> 

-->




        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/muse.js"></script>


<script src="/blog/js/next-boot.js"></script>


  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/blog/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments('#valine-comments', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', () => {
    new Valine(Object.assign({
      el  : '#valine-comments',
      path: location.pathname,
    }, {"enable":true,"appId":"dkMV7sEQR97E6CWI5jduGKf3-gzGzoHsz","appKey":"JQfXJ0ELtfNma5bdMQk0vgwX","placeholder":"ヾﾉ≧∀≦)o来啊，快活啊!","avatar":"mm","meta":["nick","mail"],"pageSize":10,"language":"zh-cn","visitor":true,"comment_count":true,"recordIP":true,"serverURLs":null,"enableQQ":false,"requiredFields":[]}
    ));
  }, window.Valine);
});
</script>

</body>
</html>
